package net.xiaowenhou.demo.controller;

import net.xiaowenhou.demo.dao.UserMapper;
import net.xiaowenhou.demo.model.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserMapper userMapper;


    @RequestMapping(value = "/user/list", method= RequestMethod.GET)
    public void testQueryAll(){
        List<User> userList = userMapper.getAll(0, 10);
        System.out.println(userList);
    }

    @RequestMapping(value = "/user/{id}", method= RequestMethod.GET)
    public void testQueryOne(@PathVariable Long id){
        User user = userMapper.getOne(id);
        System.out.println(user);
    }

    @RequestMapping(value = "/user/name/id/list", method = RequestMethod.GET)
    public void testQueryIdsByUserNameAndPassWord(){
        List<Long> idList = userMapper.getIdsByUserNameAndPassWord("zzz", "王二麻子");

        System.out.println("get id list is : " + idList);
    }

    @RequestMapping(value = "/user/name/list", method = RequestMethod.GET)
    public void testQueryObjectsByIdList(){
        List<Long> idList = userMapper.getIdsByUserNameAndPassWord("zzz", "王二麻子");

        List<User> userList = userMapper.getObjectsByIdList(idList);
        System.out.println("get user list is : " + userList);
    }

    public void testQueryObjectsByUserNameAndPassWord(){
        List<User> userList = userMapper.getObjectsByUserNameAndPassWord("zzz", "王二麻子");

        System.out.println("get user list is : " + userList);
    }

    public void testBachInsertUser(){
        List<User> userList = new ArrayList<>();
        User user1 = new User("zhangsan", "admin", "male", "Jack");
        User user2 = new User("lisi", "password", "female", "David");
        User user3 = new User("wangwu", "hello", "male", "Joe");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);

        int length = userMapper.bachInsert(userList);
        System.out.println("================================bachInsert: " + length);
    }

    public void testInsertUser(){
        User user3 = new User("zzz", "hello", "male", "Joe");

        System.out.println("====================before insert: " + user3.getId());

        Long id = userMapper.insert(user3);
        System.out.println("================================insert: " + id);
        System.out.println("====================after insert: " + user3.getId());
    }

    public void testCountUser(){
        Long count = userMapper.count();

        System.out.println("count: " + count);
    }

    public void testUpdate(){
        User user = userMapper.getOne(31L);
        System.out.println("before update: " + user);

        user.setPassWord("王二麻子");

        int result = userMapper.update(user);

        System.out.println("result : "+ result);
        System.out.println(userMapper.getOne(31L));
    }

    public void testBachUpdate(){
        List<User> userList = userMapper.getAll(0, 10);

        for (int i=0; i<userList.size(); i++){
            userList.get(i).setPassWord("脉动");
        }

        System.out.println("------------------------------ before update: " + userList.size());
        int result = userMapper.bachUpdate(userList);

        System.out.println("bachUpdate : " + result);
    }

    public void testDelete(){
        Boolean result = userMapper.delete(44L);
    }

    public void testBachDelete(){
        List<Long> idList = new ArrayList<>();
        idList.add(36L);
        idList.add(37L);
        idList.add(38L);
        idList.add(39L);
        Boolean result = userMapper.bachDelete(idList);

    }

}
